package com.ruoyi.attendance.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.attendance.config.QtCodeConfig;
import com.ruoyi.attendance.domain.AttendanceInfo;
import com.ruoyi.attendance.service.IAttendanceInfoService;
import com.ruoyi.attendance.service.impl.AttendanceInfoServiceImpl;
import com.ruoyi.course.domain.CourseRecord;
import com.ruoyi.course.service.ICourseRecordService;
import com.ruoyi.course.service.ICourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.attendance.domain.AttendanceRecord;
import com.ruoyi.attendance.service.IAttendanceRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考勤信息Controller
 * 
 * @author Weiney
 * @date 2025-03-03
 */
@RestController
@RequestMapping("/attendanceManagement/signRecord")
public class AttendanceRecordController extends BaseController
{
    @Autowired
    private IAttendanceRecordService attendanceRecordService;
    @Autowired
    private ICourseRecordService courseRecordService;
    @Autowired
    private IAttendanceInfoService attendanceInfoService;
    @Autowired
    private QtCodeConfig qtCodeConfig;
    private static final Logger log = LoggerFactory.getLogger(AttendanceRecordController.class);

    /**
     * 【学生】查询考勤信息列表
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(AttendanceRecord attendanceRecord)
    {
        startPage();
        attendanceRecord.setUserId(getUserId());
        List<AttendanceRecord> list = attendanceRecordService.selectAttendanceRecordListForStudent(attendanceRecord);
        return getDataTable(list);
    }

    /**
     * 【教师】查询学生考勤信息
     * @param attendanceRecord
     * @return
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:teacher:list')")
    @GetMapping("/teacher/list")
    public TableDataInfo listForTeacher(AttendanceRecord attendanceRecord){
        startPage();
        List<AttendanceRecord> list = attendanceRecordService.selectAttendanceRecordListForTeacher(attendanceRecord);
        return getDataTable(list);
    }

    /**
     * 【教师】统计学生考勤信息
     * @param attendanceRecord
     * @return
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:teacher:analysis')")
    @GetMapping("/teacher/analysis")
    public TableDataInfo analysisForTeacher(AttendanceRecord attendanceRecord){
        List<AttendanceRecord> list = attendanceRecordService.selectAttendanceRecordListForTeacher(attendanceRecord);
        return getDataTable(list);
    }

    /**
     * 考勤详情查询
     * @param attendanceRecord
     * @return
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:teacher:signDetail')")
    @GetMapping("/teacher/signDetail")
    public TableDataInfo listForSignDetail(AttendanceRecord attendanceRecord){
        startPage();
        CourseRecord courseRecord = courseRecordService.getStartCourseRecordByCourseId(attendanceRecord.getCourseId());
        Long courseRecordId = courseRecord.getId();
        attendanceRecord.setCourseRecordId(courseRecordId);
        List<AttendanceRecord> list = attendanceRecordService.selectAttendanceRecordListForDetail(attendanceRecord);
        return getDataTable(list);
    }

    /**
     * 根据courseId获取考勤记录ID
     * @param courseId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:teacher:courseRecordId')")
    @GetMapping("/teacher/getCourseRecordId")
    public AjaxResult getCourseRecordIdByCourseId(@RequestParam Long courseId){
        CourseRecord courseRecord = courseRecordService.getStartCourseRecordByCourseId(courseId);
        return AjaxResult.success(courseRecord.getId());
    }

    /**
     * 获取二维码链接
     * @return
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:teacher:scanLink')")
    @GetMapping("/teacher/getCodeLink")
    public AjaxResult getCodeLink(){
        log.info("获取二维码链接{}", qtCodeConfig.getCodeLink());
        String codeLink = qtCodeConfig.getCodeLink();
        return AjaxResult.success(codeLink);
    }

    /**
     * 根据courseId获取考勤类型
     * @param courseId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:teacher:attendanceType')")
    @GetMapping("/teacher/getAttendanceType")
    public AjaxResult getAttendanceTypeByCourseId(@RequestParam Long courseId){
        CourseRecord courseRecord = courseRecordService.getStartCourseRecordByCourseId(courseId);
        AttendanceInfo attendanceInfo = attendanceInfoService.selectAttendanceInfoByCourseRecordId(courseRecord.getId());
        return AjaxResult.success(attendanceInfo.getAttendanceType());
    }

    /**
     * 导出考勤信息列表
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:exportForTeacher')")
    @Log(title = "考勤信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttendanceRecord attendanceRecord)
    {
        List<AttendanceRecord> list = attendanceRecordService.selectAttendanceRecordListForTeacher(attendanceRecord);
        ExcelUtil<AttendanceRecord> util = new ExcelUtil<AttendanceRecord>(AttendanceRecord.class);
        util.exportExcel(response, list, "考勤信息数据");
    }

    /**
     * 获取考勤信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:queryForTeacher')")
    @GetMapping
    public AjaxResult getInfo(@RequestParam("userId") Long userId, @RequestParam("courseRecordId") Long courseRecordId) {
        return success(attendanceRecordService.selectAttendanceRecordByUserIdAndCourseRecordId(userId, courseRecordId));
    }

    /**
     * 新增考勤信息
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:add')")
    @Log(title = "考勤信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttendanceRecord attendanceRecord)
    {
        attendanceRecord.setUserId(getUserId());
        attendanceRecord.setDeptId(getDeptId());
        return toAjax(attendanceRecordService.insertAttendanceRecord(attendanceRecord));
    }

    /**
     * 修改考勤信息
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:editForTeacher')")
    @Log(title = "考勤信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttendanceRecord attendanceRecord)
    {
        return toAjax(attendanceRecordService.updateAttendanceRecord(attendanceRecord));
    }

    /**
     * 删除考勤信息
     */
    @PreAuthorize("@ss.hasPermi('attendanceManagement:signRecord:remove')")
    @Log(title = "考勤信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courseRecordIds}")
    public AjaxResult remove(@PathVariable Long[] courseRecordIds)
    {
        return toAjax(attendanceRecordService.deleteAttendanceRecordByCourseRecordIds(courseRecordIds));
    }
}
